package com.sharemedia.views
{
	import com.sharemedia.events.TrackEvent;
	import com.sharemedia.interfaces.IAudioManager;
	import com.sharemedia.managers.TrackList;
	import com.sharemedia.skins.defaultSkin.PlayListSkin;
	import com.sharemedia.utils.SkinUtil;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	
	import spark.components.Button;
	import spark.components.List;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.events.IndexChangeEvent;
	
	[Event(name="trackSelected", type="com.sharemedia.events.TrackEvent")]
	public class Playlist extends SkinnableComponent
	{
		// Set default skin
		SkinUtil.setSkin("com.sharemedia.views.Playlist", PlayListSkin);
		
		[SkinPart]
		public var list:List;
		
		[SkinPart]
		public var addButton:Button;
		
		[SkinPart]
		public var removeButton:Button;
		
		[SkinPart]
		public var saveButton:Button;
		
		public function Playlist()
		{
			super();
		}
		
		private var _audioManager:IAudioManager; 

		public function get audioManager():IAudioManager
		{
			return _audioManager;
		}

		public function set audioManager(value:IAudioManager):void
		{
			if(_audioManager != value)
			{
				if(_audioManager)
					_audioManager.removeEventListener(TrackEvent.PLAY, playHandler);
				
				_audioManager = value;
			
				if(list && _audioManager)
					list.dataProvider = _audioManager.trackList.list;
				
				if(_audioManager)
					_audioManager.addEventListener(TrackEvent.PLAY, playHandler);

			}
		}
		
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			switch(instance)
			{
				case list:
				{
					if(_audioManager)
						list.dataProvider = _audioManager.trackList.list;
					list.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);	
					list.addEventListener(IndexChangeEvent.CHANGE, selectHandler);	
					
					if(_audioManager)
						list.selectedItem = _audioManager.currentTrack;
					
					if(removeButton)
						removeButton.enabled = list.selectedIndex >= 0;
					break;
				}
				case addButton:
				{
					addButton.addEventListener(MouseEvent.CLICK, addTrackHandler);
					break;
				}
				case removeButton:
				{
					removeButton.addEventListener(MouseEvent.CLICK, removeTrackHandler);
					if(list)
						removeButton.enabled = list.selectedIndex >= 0;
					break;
				}
				case saveButton:
				{
					saveButton.addEventListener(MouseEvent.CLICK, saveTrackHandler);
					break;
				}
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			
			switch(instance)
			{
				case list:
				{
					list.removeEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
					list.removeEventListener(IndexChangeEvent.CHANGE, selectHandler);	
					break;
				}
				case addButton:
				{
					addButton.removeEventListener(MouseEvent.CLICK, addTrackHandler);
					break;
				}
				case removeButton:
				{
					removeButton.removeEventListener(MouseEvent.CLICK, removeTrackHandler);
					break;
				}
				case saveButton:
				{
					saveButton.removeEventListener(MouseEvent.CLICK, saveTrackHandler);
					break;
				}
			}
		}
		
		protected function playHandler(event:TrackEvent):void
		{
		}
		
		protected function doubleClickHandler(event:MouseEvent):void
		{
		}
		
		protected function addTrackHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
		}
		
		protected function removeTrackHandler(event:MouseEvent):void
		{
			audioManager.trackList.removeTrack(list.selectedItem);
		}
		
		protected function saveTrackHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
		}
		
		protected function selectHandler(event:IndexChangeEvent):void
		{
			if(removeButton)
				removeButton.enabled = list.selectedIndex >= 0;
		}
		
		
	}
}